"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : DMS分布式锁
Case Name   : 开启事务在备机上对同一表进行select,不会阻塞
Create At   : 2023/4/24
Owner       : opentestcase033
Description :
    1.创建测试表
    2.在不同备节点开启事务对同一表进行查询
    3.清理环境
Expect:
    1.成功
    2.执行成功，不阻塞
    3.成功
History     :
"""

import os
import unittest

from testcase.utils.Logger import Logger
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.ComThread import ComThread


class DmsOck(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.std1_sh = CommonSH('Standby1DbUser')
        self.std2_sh = CommonSH('Standby2DbUser')
        self.constant = Constant()
        self.table = 't_dms_ock_0032'

    def test_dms_ock(self):
        text1 = '-----step1:创建测试表;expect:成功-----'
        self.log.info(text1)
        create_tb = f"drop table if exists {self.table};" \
                    f"create table {self.table}(" \
                    f"id int, name varchar, source number) " \
                    f"with (segment = on);" \
                    f"insert into {self.table} values(1,'haha',92);"
        create_res = self.pri_sh.execut_db_sql(create_tb)
        self.log.info(create_res)
        self.assertIn(self.constant.CREATE_TABLE_SUCCESS, create_res,
                      f"执行失败:{text1}")
        self.assertIn(self.constant.INSERT_SUCCESS_MSG, create_res,
                      f"insert失败:{text1}")

        text2 = '-----step2:在不同备节点开启事务对同一表进行查询;' \
                'expect:执行成功，不阻塞-----'
        self.log.info(text2)
        sql1 = f"start transaction;" \
               f"select * from {self.table};" \
               f"select pg_sleep(3);"
        self.log.info(sql1)
        session1 = ComThread(self.std1_sh.execut_db_sql, args=(sql1,))
        session1.setDaemon(True)
        session1.start()

        sql2 = f'''begin;
        select locktype, relname, mode from pg_locks, pg_class 
        where relation = oid and relname = '{self.table}';
        select * from {self.table};
        end;'''
        res = self.std2_sh.execut_db_sql(sql2)
        self.log.info(res)
        self.assertTrue('1 | haha |     92' in res and
                        self.constant.COMMIT_SUCCESS_MSG in res,
                        f"执行失败:{text2}")
        session1.join(10)
        result1 = session1.get_result()
        self.log.info(f'session1_results: {result1}')
        self.assertIn(self.constant.START_TRANSACTION_SUCCESS_MSG,
                      result1, f"执行失败:{text2}")
        self.assertIn('1 | haha |     92', result1, f"执行失败:{text2}")

    def tearDown(self):
        text4 = '-----step4:清理环境;expect:成功-----'
        self.log.info(text4)
        drop_res = self.pri_sh.execut_db_sql(
            f"drop table if exists {self.table} cascade;")
        self.log.info(drop_res)
        self.assertIn(self.constant.TABLE_DROP_SUCCESS, drop_res,
                      f"删表失败:{text4}")
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
